package com.yyc.stocktake2.weight

import android.content.Context
import android.graphics.Bitmap
import android.view.View
import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.AppCompatImageView
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.ImageUtils
import com.blankj.utilcode.util.TimeUtils
import com.github.gcacace.signaturepad.views.SignaturePad
import com.lxj.xpopup.impl.FullScreenPopupView
import com.yyc.stocktake2.R
import com.yyc.stocktake2.ext.Externalpath
import java.io.File


/**
 * User: Nike 写字板
 *  2024/11/21 10:12
 */
class PopupSignatureView(context: Context) : FullScreenPopupView(context) {

    override fun getImplLayoutId(): Int = R.layout.p_signature

    override fun onCreate() {
        super.onCreate()
        var signature = findViewById<SignaturePad>(R.id.signature)

        findViewById<AppCompatButton>(R.id.bt_clear).setOnClickListener {
            signature.clear();
        }

        findViewById<AppCompatButton>(R.id.bt_save).setOnClickListener {
            val signatureBitmap: Bitmap = signature.getSignatureBitmap()
            if (signatureBitmap != null){
                var path = Externalpath + "/" + TimeUtils.getNowString() + ".png"
                val existsDir = FileUtils.createOrExistsDir(Externalpath)
                if (existsDir){
                    val save = ImageUtils.save(signatureBitmap, path, Bitmap.CompressFormat.PNG)
                    dismiss()
                    if (save){
                        val file = File(path).absolutePath
                        mItemClickListener?.onSignature(file)
                    }
                }
            }
        }

        findViewById<View>(R.id.iv_del).setOnClickListener {
            dismiss()
        }
    }



    override fun onShow() {
        super.onShow()
    }

    override fun onDismiss() {
        super.onDismiss()
    }

    var mItemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(l: OnItemClickListener?) {
        mItemClickListener = l
    }

    interface OnItemClickListener {
        fun onSignature(file: String)
    }

}